home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gmp-132.lha / gmp-1.3.2 / mpz_add.c < prev    next >
C/C++ Source or Header  |  1993-05-02  |  3KB  |  122 lines

  1. /* mpz_add -- Add two integers.
  2.  
  3. Copyright (C) 1991 Free Software Foundation, Inc.
  4.  
  5. This file is part of the GNU MP Library.
  6.  
  7. The GNU MP Library is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11.  
  12. The GNU MP Library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with the GNU MP Library; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "gmp.h"
  22. #include "gmp-impl.h"
  23.  
  24. #ifndef BERKELEY_MP
  25. void
  26. #ifdef __STDC__
  27. mpz_add (MP_INT *sum, const MP_INT *u, const MP_INT *v)
  28. #else
  29. mpz_add (sum, u, v)
  30.      MP_INT *sum;
  31.      const MP_INT *u;
  32.      const MP_INT *v;
  33. #endif
  34. #else /* BERKELEY_MP */
  35. void
  36. #ifdef __STDC__
  37. madd (const MP_INT *u, const MP_INT *v, MP_INT *sum)
  38. #else
  39. madd (u, v, sum)
  40.      const MP_INT *u;
  41.      const MP_INT *v;
  42.      MP_INT *sum;
  43. #endif
  44. #endif /* BERKELEY_MP */
  45. {
  46.   mp_srcptr up, vp;
  47.   mp_ptr sump;
  48.   mp_size usize, vsize, sumsize;
  49.   mp_size abs_usize;
  50.   mp_size abs_vsize;
  51.  
  52.   usize = u->size;
  53.   vsize = v->size;
  54.   abs_usize = ABS (usize);
  55.   abs_vsize = ABS (vsize);
  56.  
  57.   if (abs_usize < abs_vsize)
  58.     {
  59.       /* Swap U and V. */
  60.       {const MP_INT *t = u; u = v; v = t;}
  61.       {mp_size t = usize; usize = vsize; vsize = t;}
  62.       {mp_size t = abs_usize; abs_usize = abs_vsize; abs_vsize = t;}
  63.     }
  64.  
  65.   /* True: abs(USIZE) >= abs(VSIZE) */
  66.  
  67.   /* If not space for sum (and possible carry), increase space.  */
  68.   sumsize = abs_usize + 1;
  69.   if (sum->alloc < sumsize)
  70.     _mpz_realloc (sum, sumsize);
  71.  
  72.   /* These must be after realloc (u or v may be the same as sum).  */
  73.   up = u->d;
  74.   vp = v->d;
  75.   sump = sum->d;
  76.  
  77.   if (usize >= 0)
  78.     {
  79.       if (vsize >= 0)
  80.     {
  81.       sumsize = mpn_add (sump, up, abs_usize, vp, abs_vsize);
  82.       if (sumsize != 0)
  83.         sump[abs_usize] = 1;
  84.       sumsize = sumsize + abs_usize;
  85.     }
  86.       else
  87.     {
  88.       /* The signs are different.  Need exact comparision to determine
  89.          which operand to subtract from which.  */
  90.       if (abs_usize == abs_vsize && mpn_cmp (up, vp, abs_usize) < 0)
  91.         sumsize = -(abs_usize
  92.             + mpn_sub (sump, vp, abs_usize, up, abs_usize));
  93.       else
  94.         sumsize = (abs_usize
  95.                + mpn_sub (sump, up, abs_usize, vp, abs_vsize));
  96.     }
  97.     }
  98.   else
  99.     {
  100.       if (vsize >= 0)
  101.     {
  102.       /* The signs are different.  Need exact comparision to determine
  103.          which operand to subtract from which.  */
  104.       if (abs_usize == abs_vsize && mpn_cmp (up, vp, abs_usize) < 0)
  105.         sumsize = (abs_usize
  106.                + mpn_sub (sump, vp, abs_usize, up, abs_usize));
  107.       else
  108.         sumsize = -(abs_usize
  109.             + mpn_sub (sump, up, abs_usize, vp, abs_vsize));
  110.     }
  111.       else
  112.     {
  113.       sumsize = mpn_add (sump, up, abs_usize, vp, abs_vsize);
  114.       if (sumsize != 0)
  115.         sump[abs_usize] = 1;
  116.       sumsize = -(sumsize + abs_usize);
  117.     }
  118.     }
  119.  
  120.   sum->size = sumsize;
  121. }
  122.